package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.common.UserPage;
import com.itheima.mapper.UsersMapper;
import com.itheima.pojo.BsUser;
import com.itheima.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper userMapper;


    @Override
    public void update(Long id, BsUser bsUser) {
        bsUser.setLastUpdateTime(LocalDateTime.now());
        userMapper.updateById(bsUser);
    }

    @Override
    public UserPage findList(Long page, Long pagesize, String username) {
        IPage<BsUser> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        if (username != null) {
            wrapper.like(BsUser::getUsername, username);
        }
        IPage<BsUser> bsUserIPage = userMapper.selectPage(p, wrapper);
        //得到总数
        long counts = bsUserIPage.getTotal();
        //得到总页数
        Long pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1;
        //封装返回值
        UserPage result = new UserPage();
        result.setList(bsUserIPage.getRecords());
        result.setCounts(counts);
        result.setPages(pages);
        result.setPage(page);
        result.setPageSize(pagesize);
        return result;
    }

    @Override
    public void add(BsUser bsUser) {
        //加密
        String password = DigestUtils.md5DigestAsHex(bsUser.getPassword().getBytes());
        bsUser.setPassword(password);
        //补全数据
        bsUser.setCreateTime(LocalDateTime.now());
        bsUser.setLastUpdateTime(LocalDateTime.now());
        bsUser.setSex("女");
        bsUser.setStatus(0);
        userMapper.insert(bsUser);
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, boolean disabled) {
        if (!disabled) {
            return;
        }
        //根据id查询对应数据
        BsUser bsUser = userMapper.selectById(id);
        //修改状态
        if (bsUser.getStatus() == 0) {
            bsUser.setStatus(1);
        } else {
            bsUser.setStatus(0);
        }
        //更改表内数据
        userMapper.updateById(bsUser);
    }

    @Override
    public List simpleList(String keyword, Integer disabled) {
        //根据条件查询数据库满足的数据
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        if (keyword!=null){
            wrapper.like(BsUser::getUsername, keyword);
        }
        if (disabled!=null){
            wrapper.eq(BsUser::getStatus, disabled);
        }
        wrapper.like(BsUser::getRole,"admin").or()
                .like(BsUser::getRole,"editor");
        List<BsUser> bsUsers = userMapper.selectList(wrapper);
        return bsUsers;
    }

    @Override
    public BsUser findById(Long id) {
        BsUser bsUser = userMapper.selectById(id);
        return bsUser;
    }

    @Override
    public void modifyPassword(Long id, String password) {
        BsUser bsUser = userMapper.selectById(id);
        String newpwd = DigestUtils.md5DigestAsHex(password.getBytes());
        bsUser.setLastUpdateTime(LocalDateTime.now());
        bsUser.setPassword(newpwd);
        userMapper.updateById(bsUser);
    }
}
